package service

import (
	"api/internal/svc"
	"context"
	"github.com/gomodule/redigo/redis"
	"strconv"
)

type MessageService struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	redis  redis.Conn
}

const (
	EDU_LISTEN_NOTIFY = "notifyListenNotify" // 消息试听通知

)

type Chat struct {
	UserId   int `json:"userId"`
	ToUserId int `json:"toUserId"`
	UserFd   int `json:"userFd"`
	ToUserFd int `json:"toUserFd"`
}

func NewMessageService(ctx context.Context, svcCtx *svc.ServiceContext, redis redis.Conn) *MessageService {
	return &MessageService{
		ctx:    ctx,
		svcCtx: svcCtx,
		redis:  redis,
	}
}

// 消息试听
// userId:用户id  phone:手机号
func (msgService *MessageService) SaveEduListen(userId int, phone int) {
	field := "user:" + strconv.Itoa(userId)
	redis := msgService.svcCtx.RedisPool.Get()
	defer redis.Close()
	// 保存
	redis.Do("HSET", EDU_LISTEN_NOTIFY, field, phone)
}

// 判断是否已经听课了 true:是 false:否  当前仅一个老师
func (msgService *MessageService) AlreadyEduListen(userId int, phone int) bool {
	field := "user:" + strconv.Itoa(userId)
	// 保存
	ret, _ := msgService.redis.Do("HGET", EDU_LISTEN_NOTIFY, field)
	if ret != nil {
		byteSlice, _ := ret.([]byte)
		str := string(byteSlice)
		v, _ := strconv.Atoi(str)
		if v == phone {
			return true
		} else {
			return false
		}
	} else {
		return false
	}

}
